package com.diandian.util;

import org.apache.log4j.Logger;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @description EmailSender
 * @author TDW
 * @date 2016年11月14日
 */
@ConfigurationProperties(prefix="sys_mail" ,locations="classpath:shop.yml")
public class EmailSender {

	private static Logger log = Logger.getLogger(EmailSender.class);

	/** mail host */
	private static String host;//="smtp.exmail.qq.com" ;//= PropertiesUtils.getValueByKey("sys.mail.host");
	/** mail user name */
	private static String username;//="taodengwen@diandian-tech.com";// = PropertiesUtils.getValueByKey("sys.mail.username");
	/** mail user pwd */
	private static String password;// ="op2014";//= PropertiesUtils.getValueByKey("sys.mail.pwd");
	/** mail sender view name */
	private static String sendername;//="点点";// = PropertiesUtils.getValueByKey("mail.sender.name");
	/** mail default receiver */
	private static String receiver;//="taodengwen@diandian-tech.com;sifulin@diandian-tech.com;";// = PropertiesUtils.getValueByKey("sys.mail.receiver");
	/** mail title */
	private static String title;//="系统异常";// = PropertiesUtils.getValueByKey("sys.mail.title");
	/** project name */
	private static String projectName = "dd_zuul";

	
	
	private static EmailUtil mailUtil ;//= new EmailUtil(host, false, username, password, true);

	/**
	 * 发送系统错误邮件
	 * 
	 * @param content
	 */
	public static void sendSystemException(String content) {
		send(receiver, (title+"-"+projectName), content);
	}

	/**
	 * 发送邮件
	 * 
	 * @param receiver
	 *            收件人
	 * @param title
	 *            邮件标题
	 * @param content
	 *            邮件内容
	 */
	public static void send(String receiver, String title, String content) {
		try {
			if(mailUtil == null)
				mailUtil = new EmailUtil(host, false, username, password, true);
			mailUtil.sendEmail(username, sendername, receiver, title, content);
		} catch (Exception e) {
			log.error("发邮件时异常", e);
		}
	}

	public static void setLog(Logger log) {
		EmailSender.log = log;
	}

	public static void setHost(String host) {
		EmailSender.host = host;
	}

	public static void setUsername(String username) {
		EmailSender.username = username;
	}

	public static void setPassword(String password) {
		EmailSender.password = password;
	}

	public static void setSendername(String sendername) {
		EmailSender.sendername = sendername;
	}

	public static void setReceiver(String receiver) {
		EmailSender.receiver = receiver;
	}

	public static void setTitle(String title) {
		EmailSender.title = title;
	}

	public static void setProjectName(String projectName) {
		EmailSender.projectName = projectName;
	}

	public static void setMailUtil(EmailUtil mailUtil) {
		EmailSender.mailUtil = mailUtil;
	}
	
	
	
}
